package s19.ch22;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Converter {

	public static void main(String[] args) {
		final String[] stopnie = { "C -> F", "F -> C" };
		JFrame jFrame = new JFrame("Celsius <----> Fahrenheit");
		JPanel jPanel = new JPanel();
		BorderLayout borderLayout = new BorderLayout(200, 100);
		GridLayout gridLayout = new GridLayout();
		FlowLayout flowLayout = new FlowLayout();
		BoxLayout boxLayout = new BoxLayout(jPanel, BoxLayout.LINE_AXIS);
		jPanel.setLayout(boxLayout);
//		jFrame.setLayout(flowLayout);
//		 jFrame.setLayout(gridLayout);
//		 jFrame.setLayout(borderLayout);
		final JLabel jLabel = new JLabel();
		JButton jButton = new JButton("Convert");
		jFrame.add(jLabel, BorderLayout.SOUTH);
		jFrame.add(jPanel);
		final JTextField jTextField = new JTextField("0.00", 10);
		final JTextField jTextField2 = new JTextField(10);
		final JComboBox<String> jComboBox = new JComboBox<>(stopnie);
		jPanel.add(jComboBox);
		jPanel.add(jTextField);
		jPanel.add(jTextField2);
		jPanel.add(jButton);
		ActionListener listener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Double temp = Double.parseDouble(jTextField.getText());
				if (jComboBox.getSelectedIndex() == 0) {
					Double f = temp * 1.8 + 32;
					jLabel.setText("Celsius:     "
							+ String.format("%.2f", temp)
							+ "    Fahrenheit:   " + String.format("%.2f", f));
					jTextField2.setText(String.format("%.2f", f));
					
				} else {
					Double c = (temp - 32) / 1.8;
					jLabel.setText("Fahrenheit:   "
							+ String.format("%.2f", temp) + "    Celsius:   "
							+ String.format("%.2f", c));
					jTextField2.setText(String.format("%.2f", c));
				}
			}
		};
		jButton.addActionListener(listener);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setSize(400, 200);
		jFrame.setVisible(true);

	}

}
